package cn.eng.pet.service.impl;

import cn.eng.basic.utils.AjaxResult;
import cn.eng.basic.utils.BaiduAuditUtils;
import cn.eng.pet.domain.Pet;
import cn.eng.pet.domain.PetDetail;
import cn.eng.pet.domain.PetOnlineAuditLog;
import cn.eng.pet.mapper.PetDetailMapper;
import cn.eng.pet.mapper.PetMapper;
import cn.eng.pet.mapper.PetOnlineAuditLogMapper;
import cn.eng.pet.service.IPetService;
import cn.eng.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * 业务实现类：
 */
@Service
@SuppressWarnings("all")
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {
    @Autowired
    private PetDetailMapper petDetailMapper;
    @Autowired
    private PetMapper petMapper;
    @Autowired
    private  PetOnlineAuditLogMapper petOnlineAuditLogMapper;

    /**
     * 覆写添加方法 添加宠物时 需要添加宠物详情
     */
    @Override
    @Transactional
    public void save(Pet pet) {
        super.save(pet);
        PetDetail petDetail = pet.getPetDetail();
        if (petDetail != null){
            petDetail.setPetId(pet.getId());
            petDetailMapper.save(pet.getPetDetail());
        }
    }

    /**
     * 覆写删除方法 删除宠物时 需要删除宠物详情
     */
    @Override
    @Transactional
    public void remove(Long id) {
        super.remove(id);
       petDetailMapper.deleteByPetId(id);
    }

    /**
     * 覆写修改方法 修改宠物时 需要修改宠物详情
     */
    @Override
    @Transactional
    public void update(Pet pet) {
        super.update(pet);
        if (pet.getPetDetail() != null){
            petDetailMapper.update(pet.getPetDetail());
        }
    }
    /**
     * 覆写批量删除方法 删除宠物时 需要删除宠物详情
     */
    @Override
    @Transactional
    public void patchDel(Long[] ids) {
        super.patchDel(ids);
        for (Long id:ids){
            petDetailMapper.deleteByPetId(id);
        }
    }

    /**
     * 上架
     * @param ids
     * @return
     */
    @Override
    @Transactional
    public AjaxResult onsale(Long[] ids) {
        for (Long id:ids){
            Pet pet = petMapper.loadById(id);
//            Boolean bool1 = BaiduAuditUtils.TextCensor(pet.getName());
//            Boolean bool2 = BaiduAuditUtils.ImgCensor(pet.getResources());
//            if (bool1 && bool2){
                petMapper.updateOnSaleState(id,new Date());
                //添加审核日志
                PetOnlineAuditLog log = new PetOnlineAuditLog();
                log.setPetId(id);
                log.setState(1);
                log.setAuditTime(new Date());
                log.setNote("审核通过");
                petOnlineAuditLogMapper.save(log);
//            }else {
//                //添加审核日志
//                PetOnlineAuditLog log = new PetOnlineAuditLog();
//                log.setPetId(id);
//                log.setState(0);
//                log.setAuditTime(new Date());
//                log.setNote("审核失败");
//                petOnlineAuditLogMapper.save(log);
//                return AjaxResult.me().setSuccess(false).setMsg("审核失败");
//            }
        }
        return AjaxResult.me();
    }

    /**
     * 下架
     * @param ids
     * @return
     */
    @Override
    @Transactional
    public AjaxResult offsale(Long[] ids) {
        petMapper.updateOffsaleState(ids,new Date());
        return AjaxResult.me();
    }
}
